package com.bicon.botu.mall.goods.rpc.service;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.dto.GlobalSearchDTO;
import com.bicon.botu.mall.goods.commons.GoodsConstants;
import com.bicon.botu.mall.goods.dto.OrderVerifyDto;
import com.bicon.botu.mall.goods.dto.ProductRepertoryIdListDto;
import com.bicon.botu.mall.goods.entity.ProductComment;
import com.bicon.botu.mall.goods.service.IProductService;
import com.bicon.botu.mall.goods.vo.ProductDrugVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品搜索rpc
 *
 * @author tandk
 * @date 2017/9/8 0008 下午 16:05
 */
@Service
public class ProductRpc extends BaseController {


    @Autowired
    private IProductService productService;

    /**
     * 商品搜索
     *
     * @param searchWord 搜索关键字
     * @param pageSize   显示条数
     * @param pageNo     当前页码
     * @return
     */
    @RequestMapping(value = "searchMallGoods", method = RequestMethod.POST)
    public Page<GlobalSearchDTO> getProductList(@RequestParam String searchWord, @RequestParam("pageSize") Integer pageSize, @RequestParam("pageNo") Integer pageNo) {
        Map<String, Object> map = new HashMap<>();
        Page page = new Page(pageNo, pageSize);
        if (!StringUtils.isEmpty(searchWord)) {
            map.put("productName", "%" + searchWord + "%");
        }
        return productService.getProductListForRpc(page, map);
    }

    /**
     * 商品搜索
     *
     * @param searchWord 搜索关键字
     * @param pageSize   显示条数
     * @param pageNo     当前页码
     * @return
     */
    @RequestMapping(value = "/searchDrug", method = RequestMethod.POST)
    public BaseResult selectProductList(@RequestParam("searchWord") String searchWord,
                                        @RequestParam("pageSize") Integer pageSize,
                                        @RequestParam("pageNo") Integer pageNo) {
        Map<String, Object> map = new HashMap<>();
        Page<ProductDrugVo> page = new Page(pageNo, pageSize);
        if (StringUtils.isNotBlank(searchWord)) {
            map.put("searchWord", "%" + searchWord + "%");
        }
        List<ProductDrugVo> productDrugVoList = productService.selectProductList(page, map);
        page.setRecords(productDrugVoList);
        return sendResult200(page);
    }

    /**
     * 根据库存id查询商品的信息
     *
     * @param productRepertoryIdArrays  多个商品库存id
     * @return
     */
    @RequestMapping(value = "/productRepertory", method = RequestMethod.POST)
    public List<ProductRepertoryIdListDto> selectProductList(@RequestParam("productRepertoryIdList") String[] productRepertoryIdArrays) {

        List<ProductRepertoryIdListDto> list = productService.selectProductRepertory(Arrays.asList(productRepertoryIdArrays));
        return list;
    }

    /**
     * 根据商品信息验证商品
     *
     * @param verifyDtoListJson  多个商品库存
     * @return
     */
    @RequestMapping(value = "/verifyProduct", method = RequestMethod.POST)
    public Boolean verifyProduct(@RequestParam(value = "verifyDtoList") String verifyDtoListJson) {
        Boolean result = productService.verifyProduct(verifyDtoListJson);
        return result;
    }


//    @RequestMapping(value = "/reduceProduct", method = RequestMethod.POST)
//    public Boolean reduceProduct(@RequestParam(value = "reduceProduct") List<OrderVerifyDto> verifyDtoList) {
//        Boolean result = productService.reduceProduct(verifyDtoList);
//        return result;
//    }

    @RequestMapping("shelvesTotal")
    public BaseResult shelvesTotal(){
        return sendResult(GoodsConstants.RESPONSE_CODE_200, GoodsConstants.RESPONSE_MESSAGE_200, this.productService.goodsStatistic());
    }
}
